package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.base.util.PasswordUtil;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service  //表示当前类为此接口对应的实现类，注入该类对象
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam){
        log.debug("用户登录业务:{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        /* 如果业务层遇到了问题，不能直接返回结果给前端，而是要抛出业务层异常，让全局异常处理器来处理
        * 一旦抛出异常，结果是由全局异常处理器来返回给前端的，就不走controller了 */
        if(userVO==null){ //用户不存在
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){ //密码错误
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("当前登录用户数据:{}",userVO);
        return userVO;
    }
    @Override
    public List<UserVO> selectUser(UserQuery userQuery){
        log.debug("查询用户业务:{}",userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }
    @Override
    public void saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户业务:{}",userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if (user.getId()==null){
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("重置密码业务:{}",userId);
        User user = new User();
        user.setId(userId);
        //user.setPassword("root");
        //调用密码生成器生成一个随机的7位新密码
        user.setPassword(PasswordUtil.generateRandomPassword(7));
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("更新用户状态业务:{},{}",userId,status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除用户业务:{}",userId);
        userMapper.deleteById(userId);
    }

    @Override
    public List<UserVO> selectAuditList(Long parentId) {
        log.debug("查询待审核用户业务:{}",parentId);
        //1.准备空集合用来保存多个查出来的审批人
        ArrayList<UserVO> userVOlist = new ArrayList<>();
        //2.先跟据参数将当前登录人的直属领导查出来
        UserVO auditUser1 = userMapper.selectById(parentId);
        userVOlist.add(auditUser1);
        //3.如果有查出直属领导，且领导还有领导，再把上上及查出来
        if (auditUser1 !=null && auditUser1.getParentId()!=null){
            UserVO auditUser2 = userMapper.selectById(auditUser1.getParentId());
            userVOlist.add(auditUser2);
        }
        //4.将准备好的审批人列表返回
        return userVOlist;
    }
}
